/**
 *
 */
package org.jim.core.packets.pub.pojo;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.jim.core.packets.Message;
import org.jim.core.packets.pub.consts.UserStatusType;

import java.io.Serializable;
import java.util.List;

/**
 * 版本: [1.0]
 * 功能说明:
 *
 * @author : WChao 创建时间: 2017年7月26日 下午3:13:47
 */
public class JimUser extends Message implements Serializable {

    private static final long serialVersionUID = 1L;
    /**
     * 增加userId
     */
    private String userId;


    private String username;
    /**
     * user nick
     */
    private String nick;

    /**
     * 用户类型
     */
    private Integer type;

    /**
     * 所属租户
     */
    private String imTenantId;


    /**
     * 所属应用组
     */
    private String appGroupId;


    /**
     * 所属应用
     */
    private String appId;

    /**
     * 所属服务商
     */
    private String imSpId;

    /**
     * 所属客服组
     */
    private String userOrgId;

    /**
     * 用户头像
     */
    private String avatar;
    /**
     * 在线状态(online、offline)
     */
    private String status = UserStatusType.OFFLINE.getStatus();
    /**
     * 个性签名;
     */
    private String sign;
    /**
     * 用户所属终端;(ws、tcp、http、android、ios等)
     */
    private String terminal;

    /**
     * 备注
     */
    private String aliasName;
    /**
     * 好友列表;
     */
    private List<Group> friends;
    /**
     * 群组列表;
     */
    private List<Group> groups;

    private JimUser() {
    }

    private JimUser(String userId,String username, String nick,
                    Integer type, String imTenantId, String appGroupId, String appId, String imSpId, String userOrgId,
                    String avatar, String status, String sign, String terminal,
                    String aliasName, List<Group> friends, List<Group> groups, JSONObject extras) {
        this.userId=userId;
        this.username = username;
        this.nick = nick;

        this.type = type;
        this.imTenantId = imTenantId;
        this.appGroupId = appGroupId;
        this.appId = appId;
        this.imSpId = imSpId;
        //只有客服才有客服组
        this.userOrgId=userOrgId;

        this.avatar = avatar;
        this.status = status;
        this.sign = sign;
        this.terminal = terminal;
        this.aliasName = aliasName;
        this.friends = friends;
        this.groups = groups;
        this.extras = extras;
    }

    public static JimUser.Builder newBuilder() {
        return new JimUser.Builder();
    }

    public String getUserId() {
        return userId;
    }

    public JimUser setUserId(String userId) {
        this.userId = userId;
        return this;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getNick() {
        return nick;
    }

    public void setNick(String nick) {
        this.nick = nick;
    }

    public String getAvatar() {
        return avatar;
    }

    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public String getTerminal() {
        return terminal;
    }

    public void setTerminal(String terminal) {
        this.terminal = terminal;
    }

    public List<Group> getFriends() {
        return friends;
    }

    public void setFriends(List<Group> friends) {
        this.friends = friends;
    }

    public List<Group> getGroups() {
        return groups;
    }

    public void setGroups(List<Group> groups) {
        this.groups = groups;
    }

    public String getAliasName() {
        return aliasName;
    }

    public void setAliasName(String aliasName) {
        this.aliasName = aliasName;
    }

    public Integer getType() {
        return type;
    }

    public JimUser setType(Integer type) {
        this.type = type;
        return this;
    }

    public String getImTenantId() {
        return imTenantId;
    }

    public void setImTenantId(String imTenantId) {
        this.imTenantId = imTenantId;
    }

    public String getAppGroupId() {
        return appGroupId;
    }

    public void setAppGroupId(String appGroupId) {
        this.appGroupId = appGroupId;
    }

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getImSpId() {
        return imSpId;
    }

    public JimUser setImSpId(String imSpId) {
        this.imSpId = imSpId;
        return this;
    }

    public String getUserOrgId() {
        return userOrgId;
    }

    public JimUser setUserOrgId(String userOrgId) {
        this.userOrgId = userOrgId;
        return this;
    }

    public static class Builder extends Message.Builder<JimUser, JimUser.Builder> {
        /**
         * 用户Id
         */
        private String userId;
        /**
         * 用户名称
         */
        private String username;
        /**
         * user nick
         */
        private String nick;

        /**
         * 用户类型
         */
        private Integer type;

        /**
         * 所属租户
         */
        private String imTenantId;


        /**
         * 所属应用组
         */
        private String appGroupId;


        /**
         * 所属应用
         */
        private String appId;

        /**
         * 服务商Id
         */
        private String imSpId;

        /**
         * 客服组
         */
        private String userOrgId;
        /**
         * 用户头像
         */
        private String avatar;
        /**
         * 在线状态(online、offline)
         */
        private String status = UserStatusType.OFFLINE.getStatus();
        /**
         * 个性签名;
         */
        private String sign;
        /**
         * 用户所属终端;(ws、tcp、http、android、ios等)
         */
        private String terminal;
        /**
         * 备注
         */
        private String aliasName;
        /**
         * 好友列表;
         */
        private List<Group> friends;
        /**
         * 群组列表;
         */
        private List<Group> groups;

        public Builder() {
        }

        public JimUser.Builder userId(String userId) {
            this.userId = userId;
            return this;
        }

        public JimUser.Builder username(String username) {
            this.username = username;
            return this;
        }

        public JimUser.Builder nick(String nick) {
            this.nick = nick;
            return this;
        }


        public JimUser.Builder type(Integer type) {
            this.type = type;
            return this;
        }

        public JimUser.Builder imTenantId(String imTenantId) {
            this.imTenantId = imTenantId;
            return this;
        }

        public JimUser.Builder appGroupId(String appGroupId) {
            this.appGroupId = appGroupId;
            return this;
        }

        public JimUser.Builder appId(String appId) {
            this.appId = appId;
            return this;
        }

        public JimUser.Builder imSpId(String imSpId) {
            this.imSpId = imSpId;
            return this;
        }

        public JimUser.Builder userOrgId(String userOrgId) {
            this.userOrgId = userOrgId;
            return this;
        }

        public JimUser.Builder avatar(String avatar) {
            this.avatar = avatar;
            return this;
        }

        public JimUser.Builder status(String status) {
            this.status = status;
            return this;
        }

        public JimUser.Builder sign(String sign) {
            this.sign = sign;
            return this;
        }

        public JimUser.Builder terminal(String terminal) {
            this.terminal = terminal;
            return this;
        }

        public JimUser.Builder aliasName(String aliasName) {
            this.aliasName = aliasName;
            return this;
        }

        public JimUser.Builder addFriend(Group friend) {
            if (CollectionUtils.isEmpty(friends)) {
                friends = Lists.newArrayList();
            }
            friends.add(friend);
            return this;
        }

        public JimUser.Builder addGroup(Group group) {
            if (CollectionUtils.isEmpty(groups)) {
                groups = Lists.newArrayList();
            }
            groups.add(group);
            return this;
        }


        @Override
        protected JimUser.Builder getThis() {
            return this;
        }

        @Override
        public JimUser build() {
            return new JimUser(userId,username, nick, type, imTenantId, appGroupId, appId, imSpId,userOrgId,
                    avatar, status, sign, terminal, aliasName, friends, groups, extras);
        }

    }

    public JimUser clone() {
        JimUser cloneUser = new JimUser();
        BeanUtil.copyProperties(this, cloneUser, "friends", "groups");
        return cloneUser;
    }

}
